package com.fingard.dsp.bank.directbank.bocom01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOCOM01Deposit extends BOCOM01Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            // 赋值通用参数
            tr_code = "410101";
            atom_tr_count = "1";// 获取总共有几个帐号
            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            // 写入通用的头部节点
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("body");
            xmlWriter.writeElementString("acno", p_req7507.actNum);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/head/particular_code");
                    if (isActiveRetCode(tmpRetCode)) {
                        String serial_record = tmpBkReader.getFirstText("/ap/body/serial_record");
                        int FieldNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/field_num"));
                        int RecordNum = Integer.parseInt(tmpBkReader.getFirstText("/ap/body/record_num"));
                        java.util.List<String[]> listStrings = DealMFSText(serial_record, '|', FieldNum, RecordNum);
                        if (listStrings.size() > 0) {
                            listStrings.remove(0);
                            for (String[] strings : listStrings) {
                                //0 存单号(C8)|1 币种(C3)|2 存期(C4)|3 起息日(C8)|4 到期日(C8)|5 余额(N14.2)|6 到期利息(N14.2)|7 冻结标志(C1)|8 暂封标志(C1)|9 挂失标志(C1)|10 结算户账号(C21)|
                                RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                                retAtsDetail.depositCode = strings[0];
                                retAtsDetail.currencyID = convertCurToATS(strings[1]);
                                String term = "9";
                                if (strings[2].equals("1003")) {
                                    term = "3";
                                } else if (strings[2].equals("1006")) {
                                    term = "4";
                                } else if (strings[2].equals("0001")) {
                                    term = "5";
                                } else if (strings[2].equals("0002")) {
                                    term = "6";
                                } else if (strings[2].equals("0003")) {
                                    term = "7";
                                } else if (strings[2].equals("0005")) {
                                    term = "8";
                                }
                                retAtsDetail.terms=term;
                                retAtsDetail.startDate = Format.Date10Format.format(Format.Date8Format.parse(strings[3]));
                                retAtsDetail.valueDate = Format.Date10Format.format(Format.Date8Format.parse(strings[3]));
                                retAtsDetail.endDate = Format.Date10Format.format(Format.Date8Format.parse(strings[4]));
                                retAtsDetail.noteMoney = strings[5];
                                retAtsDetail.interestMoney = strings[6];
                                retAts.writeRetDetail(retAtsDetail);
                            }
                        }
                    } else {
                        tmpRespCode = "-1";
                        if (tmpBkReader.getSimpleText("iRetMsg") != null && tmpBkReader.getSimpleText("iRetMsg").length() > 0) {
                            tmpRespInfo = tmpBkReader.getSimpleText("iRetMsg");
                        } else {
                            tmpRespInfo = tmpBkReader.getSimpleText("RetMsg");
                        }
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
